package pers.cz.node;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.cz.mxgraph.MxCell;
import pers.cz.selenium.component.ButtonComponent;

import java.util.HashMap;

/**
 * @program: Reids
 * @description: 按钮节点
 * @author: Cheng Zhi
 * @create: 2023-01-09 09:52
 **/
public class ButtonNode extends AbstractNode{

    private Logger logger = LoggerFactory.getLogger(ButtonNode.class);

    private Runnable runnable;

    public ButtonNode(MxCell mxCell) {
        super(mxCell);
    }

    @Override
    public void verify() {

        System.out.println("校验参数中....");
    }

    @Override
    public void runNode(NodeCallBack callBack) throws Exception {

        RemoteWebDriver driver = getDriver();
        String fullPath = mxCell.getMxObjectData().get("fullPath");
        String[] splits = fullPath.split(";");
        boolean flag = false;
        for (String split : splits) {
            if (dealClick(split, driver)) {
                flag = true;
                break;
            }
        }

        if (!flag) {
            throw new RuntimeException("按钮点击失败");
        }


    }

    private boolean dealClick(String fullPath, RemoteWebDriver driver) {
        boolean flag = false;
        try {
            ButtonComponent buttonComponent = new ButtonComponent(fullPath, driver);
            if ("1".equals(mxCell.getMxObjectData().get("operation"))) {
                buttonComponent.click();
            } else if ("2".equals(mxCell.getMxObjectData().get("operation"))) {
                buttonComponent.dbClick();
            } else if ("3".equals(mxCell.getMxObjectData().get("operation"))) {
                buttonComponent.setEnter();
            }
            flag = true;
        } catch (Exception e) {
            logger.error("处理点击事件异常", e);
        }

        return flag;
    }
    @Override
    public String getName() {
        return mxCell.getName();
    }
}
